package com.zhs.gccs.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.zhs.gccs.BuildConfig;
import com.zhs.gccs.R;
import com.zhs.gccs.dao.TaskGccjFileDao;
import com.zhs.gccs.model.CaData;
import com.zhs.gccs.model.CaFile;
import com.zhs.gccs.utils.SharedPrefHelper;

import java.util.List;

/**
 * Created by 任伟伟
 * Datetime: 2016/11/18-11:07
 * Email: renweiwei@ufashion.com
 */

public class GccsDataItemAdapter extends RecyclerView.Adapter<GccsDataItemAdapter.ViewHolder> {
    private Context mContext;
    private List<CaData> datas = null;
    private TaskGccjFileDao taskGccjFileDao;

    public GccsDataItemAdapter(Context context, List<CaData> datas) {
        this.datas = datas;
        this.mContext = context;
        taskGccjFileDao = new TaskGccjFileDao();
    }
    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_gccs_data_item,viewGroup,false);
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }
    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, @SuppressLint("RecyclerView") final int position) {
        CaData data = datas.get(position);
        List<CaFile> caFiles = taskGccjFileDao.findByDataId(data.getId());
        if(caFiles!=null&&caFiles.size()>0){
            CaFile caFile = caFiles.get(0);
            String url = "";
            if(CaFile.type_image.equals(caFile.getFileType())){
                url = caFile.getLocalPath();
            }else if(CaFile.type_video.equals(caFile.getFileType())){
                url = caFile.getVideoImgUrl();
            }
            Glide.with(mContext).load(url)
                    .error(R.mipmap.photograph)
                    .into(viewHolder.iv_file);
        }else {
            if(TextUtils.isEmpty(data.getFileId())){
                Glide.with(mContext).load(R.mipmap.photograph).into(viewHolder.iv_file);
            }else {
                Glide.with(mContext).
                        load(BuildConfig.BASEFILEURL+data.getFileId()
                                +"?token="+ SharedPrefHelper.getInstance().getToken())
                        .error(R.mipmap.photograph)
                        .into(viewHolder.iv_file);
            }
        }

        viewHolder.tv_title.setText(data.getDictName()+":");//
        viewHolder.tv_input_val.setText(data.getVal());
        viewHolder.tv_remarks.setText(data.getRemarks());
        viewHolder.tv_result_fh.setText("0".equals(data.getCjjg())?"不符合":"符合");
        viewHolder.ll_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(operationListener!=null)operationListener.onItemClick(position);
            }
        });
    }
    //获取数据的数量
    @Override
    public int getItemCount() {
        return datas!=null?datas.size():0;
    }
    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public View ll_item;
        public ImageView iv_file;
        public TextView tv_title;
        public TextView tv_input_val;
        public TextView tv_result_fh;
        public TextView tv_remarks;
        public TextView tv_count;
        public ViewHolder(View view){
            super(view);
            ll_item =   view.findViewById(R.id.ll_item);
            iv_file =   view.findViewById(R.id.iv_file);
            tv_title =   view.findViewById(R.id.tv_title);
            tv_input_val =   view.findViewById(R.id.tv_input_val);
            tv_result_fh =   view.findViewById(R.id.tv_result_fh);
            tv_remarks =   view.findViewById(R.id.tv_remarks);
            tv_count =   view.findViewById(R.id.tv_count);
        }
    }
    public static interface OperationListener{
        void onItemClick(int position);
    }
    private OperationListener operationListener;

    public OperationListener getOperationListener() {
        return operationListener;
    }

    public void setOperationListener(OperationListener operationListener) {
        this.operationListener = operationListener;
    }
}
